<?php
declare (strict_types=1);

namespace app\controller\home;

use app\common\controller\BaseController;
use app\model\home\Duty;
use app\model\home\Room as roomModel;
use think\App;
use \Throwable;

class Room extends BaseController
{
    protected $model;

    public function __construct(App $app, roomModel $model)
    {
        parent::__construct($app);
        $this->model = $model;
    }

    /**
     * 列表查询
     * @return array
     */
    public function query()
    {
        $where = [];
        $where[] = ['status', '=', 1];
        $page = input('page/d', 1);
        $pageSize = input('page_size/d', 20);
        $storeId = $this->params['store_id'];
        $where[] = ['store_id', '=', $this->params['store_id']];
        $dutyDate = input('duty_date');
        empty($dutyDate) && $dutyDate = date('Y-m-d', time());
        //获取当天的包间
        $map['store_id'] = $storeId;
        $map['duty_date'] = $dutyDate;
        $map['re_type'] = 2;
        $roomIds = Duty::where($map)->column('re_id');
        if (empty($roomIds)) {
            $params = [];
        } else {
            $roomTitle = input('room_title');
            $params[] = ['store_id', '=', $storeId];
            if (!empty($roomTitle)) {
                $params[] = ['room_title', 'like', '%' . $roomTitle . '%'];
            }
            if (!empty($roomIds) && is_array($roomIds)) {
                $params[] = ['id', 'in', $roomIds];
            }
        }
        $order = null;
        $fields = 'id,room_title,room_type,store_id,room_cover,
        num,games gamesArr,start_time,start_time start_at,end_time,end_time end_at';
        return $this->model->getList($where, $fields, $order, $page, $pageSize, $params);
    }

    /**
     * @return mixed
     * 详情
     */
    public function detail()
    {
        $this->params['member_id'] = $this->memberId;
        return $this->model->getOne($this->params);
    }
}
